<?php 



//年级课程公共方法 无限分类找子孙树
function gettree($arr,$id=0,$lev=0){
   $tree = array();
   foreach ($arr as $v) {
   	if ($v['parent_id']==$id) {
   		$v['lev'] = $lev;
   		$tree[] = $v;

   		$tree = array_merge($tree,gettree($arr,$v['gradeid'],$lev+1));
   	}
   }
   return $tree;
}

//城市区域公共方法 无限分类找子孙树
function citytree($arr,$id=0,$lev=0){
   $tree = array();
   foreach ($arr as $v) {
   	if ($v['parent_id']==$id) {
   		$v['lev'] = $lev;
   		$tree[] = $v;

   		$tree = array_merge($tree,gettree($arr,$v['cityid'],$lev+1));
   	}
   }
   return $tree;
}


//分页类 年级匹配 转 (1,2,3,4)字段  跟id匹配
//$str string 存gradeid
function to_str($str)
{
   $arr = explode(',', $str);

   $grade = M('grade')->where('parent_id=0')->select();

   $str_to_arr = array();

   foreach ($grade as $v) {
      foreach ($arr as $a) {
         if ($v['gradeid'] == $a) {
            $str_to_arr[] = $v['grade_name'];
         }
      }
   }
   $str_to_arr = implode(' ', $str_to_arr);
   return $str_to_arr;
}
//分页类 年级匹配 根据(1,2,3,4)字段 区分高中初中小学 特色课程
//$str string 存gradeid
function get_grade($str)
{
  $arr_gaozhong = array(1,2,3,4);
  $arr_chuzhong = array(5,6,7,8);
  $arr_xiaoxue = array(9,10,11,12,13,14,15);
  $arr_tese = array(48);

  $arr = explode(',', $str);

  //高中
  for($i=0;$i < count($arr_gaozhong);++$i)
      if(in_array($arr_gaozhong[$i],$arr))
          ++$count_gz;
  //初中
  for($i=0;$i < count($arr_chuzhong);++$i)
      if(in_array($arr_chuzhong[$i],$arr))
          ++$count_cz;
  //小学
  for($i=0;$i < count($arr_xiaoxue);++$i)
      if(in_array($arr_xiaoxue[$i],$arr))
          ++$count_xx;
  //特色课程
  for($i=0;$i < count($arr_tese);++$i)
      if(in_array($arr_tese[$i],$arr))
          ++$count_ts;

    if ($count_gz) {
      return '高中';
    }elseif ($count_cz) {
      return '初中';
    }elseif ($count_xx) {
      return '小学';
    }elseif ($count_ts) {
      return '特色课程';
    }


}


/**
 * Excel导出
 * @param data 导出数据
 * @param title 表格的字段名 字段长度有限制，一般都够用，可以改变 $length1和$length2来增长
 * @return subject 表格主题 命名为主题+导出日期
 */
function exportExcel($data,$title,$subject){  
    Vendor('phpexcel.PHPExcel');
    Vendor('phpexcel.PHPExcel.IOFactory');
    Vendor('phpexcel.PHPExcel.Reader.Excel5');
    // Create new PHPExcel object  
    $objPHPExcel = new PHPExcel();  
    // Set properties  
    $objPHPExcel->getProperties()->setCreator("ctos")  
        ->setLastModifiedBy("ctos")  
        ->setTitle("Office 2007 XLSX Test Document")  
        ->setSubject("Office 2007 XLSX Test Document")  
        ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")  
        ->setKeywords("office 2007 openxml php")  
        ->setCategory("Test result file");  
    $length1=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD');
    $length2=array('A1','B1','C1','D1','E1','F1','G1','H1','I1','J1','K1','L1','M1','N1','O1','P1','Q1','R1','S1','T1','U1','V1','W1','X1','Y1','Z1','AA1','AB1','AC1','AD1');
    //set width  
    for($a=0;$a<count($title);$a++){
         $objPHPExcel->getActiveSheet()->getColumnDimension($length1[$a])->setWidth(20); 
    }
    //set font size bold  
    $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);  
    $objPHPExcel->getActiveSheet()->getStyle($length2[0].':'.$length2[count($title)-1])->getFont()->setBold(true); 
    $objPHPExcel->getActiveSheet()->getStyle($length2[0].':'.$length2[count($title)-1])->getFont()->setBold(true);    
    $objPHPExcel->getActiveSheet()->getStyle($length2[0].':'.$length2[count($title)-1])->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);  
    
    // set table header content  
    for($a=0;$a<count($title);$a++){
          $objPHPExcel->setActiveSheetIndex(0)->setCellValue($length2[$a], $title[$a]); 
    }
    for($i=0;$i<count($data);$i++){ 
        $buffer=$data[$i];
        $number=0;
        foreach ($buffer as $value) {
            $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($length1[$number].($i+2),$value,PHPExcel_Cell_DataType::TYPE_STRING);//设置单元格为文本格式
            $number++;
        }
        unset($value);
        $objPHPExcel->getActiveSheet()->getStyle($length1[0].($i+2).':'.$length1[$number-1].($i+2))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);  
        $objPHPExcel->getActiveSheet()->getStyle($length1[0].($i+2).':'.$length1[$number-1].($i+2))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);  
        $objPHPExcel->getActiveSheet()->getRowDimension($i+2)->setRowHeight(16);  
    }  
    // Set active sheet index to the first sheet, so Excel opens this as the first sheet  
    $objPHPExcel->setActiveSheetIndex(0);  

    ob_end_clean();//清除缓冲区,避免乱码
    // Redirect output to a client’s web browser (Excel5)  
    header('Content-Type: application/vnd.ms-excel');  
    header('Content-Disposition: attachment;filename='.$subject.'('.date('Y-m-d').').xls');  
    header('Cache-Control: max-age=0');  

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
    $objWriter->save('php://output');  
}  


//手机发短信

//随机验证码
 function sj($num)
 {   
     $sj="";
     for ($i=0; $i <$num ; $i++) { 

         $sj.=mt_rand(0,9);
     }
     return $sj;
 }

 /*获取post数据*/
 function wpost($url,$data)
 {   
     $ch=curl_init();//初始化

     curl_setopt($ch, CURLOPT_URL, $url);

     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

     curl_setopt($ch, CURLOPT_HEADER, 0);//设置header


     // post数据
     curl_setopt($ch, CURLOPT_POST, 1);
     // post的变量
     curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
     curl_setopt($ch, CURLOPT_HTTPHEADER);
     //取消https
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
     $output = curl_exec($ch);
     curl_close($ch);
     
     return json_decode($output,true);
 }

//秒滴短信发送开始----------------------
  /*
    直接调用此方法并传入手机号
    模板：请在秒滴后台新建模板并审核通过 一般几分钟通过 ，复制你的模板内容替换掉下面的模板 
    记得复制您的签名到 短信最前头 如：下面的 【泡沫科技】
  */
  function miaodi($to)
  { 
    //$to = I('get.to')?I('get.to'):null;
    date_default_timezone_set("Asia/Shanghai");
    $time=date("YmdHis");//时间戳格式

    $accountSid="f891c685b68941408a29e923f864f734";//appid
    $AUTHTOKEN="9c7257a529584f3c82257029172c2475";//密钥

    $str= $accountSid. $AUTHTOKEN."$time";//签名
    $md5=md5($str);//加密签名
    $sj = sj(4);
    $_SESSION["sj_mobile"] = $sj;

    $h=2;//几分钟过期
    $moban="【老师上门】您的验证码为".$sj."，请于".$h."分钟内正确输入，如非本人操作，请忽略此短信。";//秒滴后台定义的模板

    // $to="15768215345";//需要发送的手机号

    // 需要发送的数据
    $arr=array(
         'accountSid'=>$accountSid,//appid
         "smsContent"=>$moban,//短信内容
         "to"=>$to,//手机号
         "timestamp"=>$time,//时间
         "sig"=>$md5,//签名
         // 'respDataType'=>$respDataType,
         );

    $url="https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
    //var_dump(wpost($url,$arr));
    wpost($url,$arr);
  }

//剩余课时小于等于3时  发短信通知
  function kes_miaodi($to)
  { 
    //$to = I('get.to')?I('get.to'):null;
    date_default_timezone_set("Asia/Shanghai");
    $time=date("YmdHis");//时间戳格式

    $accountSid="f891c685b68941408a29e923f864f734";//appid
    $AUTHTOKEN="9c7257a529584f3c82257029172c2475";//密钥

    $str= $accountSid. $AUTHTOKEN."$time";//签名
    $md5=md5($str);//加密签名

    $moban="【老师上门】您的课时已所剩不多，如需续课请及时联系课程顾问续课。";//秒滴后台定义的模板

    // $to="15768215345";//需要发送的手机号

    // 需要发送的数据
    $arr=array(
         'accountSid'=>$accountSid,//appid
         "smsContent"=>$moban,//短信内容
         "to"=>$to,//手机号
         "timestamp"=>$time,//时间
         "sig"=>$md5,//签名
         // 'respDataType'=>$respDataType,
         );

    $url="https://api.miaodiyun.com/20150822/industrySMS/sendSMS";
    //var_dump(wpost($url,$arr));
    wpost($url,$arr);
  }


    


?>